package com.csii.mytest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.csii.mytest.boardcastreceiver.MyBroadcastReceiver;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 顾博君 on 2017/3/2.
 */

public class BroadcastReceiverActivity extends Activity {
    public static final String BROADCAST_ACTION = "com.example.corn";
    @BindView(R.id.btn_send)
    Button btnSend;
    @BindView(R.id.btn_send2)
    Button btnSend2;
    private BroadcastReceiver mBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局
        setContentView(R.layout.layout_activity_br);
        ButterKnife.bind(this);
        mBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        Toast.makeText(BroadcastReceiverActivity.this,
                "广播默认priority:" + intentFilter.getPriority(),
                Toast.LENGTH_LONG).show();
        intentFilter.addAction(BROADCAST_ACTION);
        registerReceiver(mBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    @OnClick({R.id.btn_send, R.id.btn_send2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_send:
                Intent intent = new Intent();
                intent.setAction(BROADCAST_ACTION);
                intent.putExtra("name", "qqyumidi");
                sendBroadcast(intent);
                break;
            case R.id.btn_send2:
                Intent intent2 = new Intent();
                intent2.setAction("android.net.conn.AA");
                intent2.putExtra("name", "broadcast2");
                sendBroadcast(intent2);
                break;
        }
    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, BroadcastReceiverActivity.class);
        context.startActivity(intent);
    }
}
